<template>
  <div class="main">
    <el-dialog
      :title="form._id ? '修改' : '新增'"
      :visible.sync="dialogFormVisible"
      width="400px"
    >
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="品牌名称" prop="name">
          <el-input type="text" v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="图片" prop="img">
            <div @click="openFile" class="imgStyle">
              <img style="max-width:100%;max-height:100%" v-if="form.img" :src="form.img" />
              <span v-else>+</span>
            </div>
          <input ref="file" style="display:none" multiple type="file" accept="image/*" @change="changeFile" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input type="text" v-model="form.sort"></el-input>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择">
            <el-option
              v-for="type in types"
              :key="type.value"
              :label="type.label"
              :value="type.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import api from '@/api'
export default {
  name: "indexbrandEdit",
  components: {
    //HelloWorld
   
  },
  data() {
    return {
      dialogFormVisible: false,
      form: {
        _id: 0,
        name: "",
        img:"",
        // type: "brand", //banner为默认值
        // sort:""
      },
      rules: {
        name: [{ required: true, message: "品牌名称不能为空", trigger: "blur" }],
        img: [{ required: true, message: "图片不能为空", trigger: "blur" }],
      },
      types: [
        { label: "品牌", value: "brand" },
        { label: "分类", value: "variety" },
        { label: "单品", value: "sigalProduct" },
        { label: "商品", value: "product" },
      ],
      selecteds:[]
    };
  },
  methods: {
    add() {
      this.form = {
        ////初始化
        _id: 0,
        name: "",
        img:"",
        // type: "brand", //banner为默认值
        // sort:""
      };
      this.dialogFormVisible = true;
    },
    edit(row) {
      this.form = row;
      this.dialogFormVisible = true;
    },
    openFile(){
      let fileElement = this.$refs.file
      fileElement.click()
    },
    changeFile(e){
      let files = e.target.files
      let formData = new FormData()
      formData.append('file',files[0])
      this.$http({
        url:'upload',
        method:'POST',
        data:formData
      }).then(res => {
      // console.log(ret);
      var path = res.path.split('\\')
      path = path[0]+'/'+path[1]
        if(res.path) this.form.img = api.baseURL + '/' + path
      })
      // console.log(files);
    },
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.form.indexId = this.form._id;
          this.$http({
            url: "/admin/index/brand",
            method: this.form.indexId ? "PUT" : "POST", ///通过indexId判断新增还是修改
            data: this.form,
          }).then((res) => {
            if (!res.errorMsg) {
              this.dialogFormVisible = false;
              this.$emit('load',this.form.indexId ? undefined : 1)
            }
          });
        }
      });
    },
  },
  created() {
  },
  mounted() {},
};
</script>

<style>
/* scoped表示样式仅对当前页面生效 */

.imgStyle {
  width: 100px;
  height: 100px;
  font-size: 32px;
  border: 1px solid #ccc;
  text-align: center;
  line-height: 100px;
  overflow: hidden;
}
</style>